﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using System.Web.Services;

namespace XHCTMS.VIEW
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form.Count != 0)
            {
                int models = int.Parse(Request.Form["model"].ToString());
                switch (models)
                {
                    case 1: View();
                        break;
                    case 2: Add();
                        break;
                    case 3: Delete();
                        break;
                    case 4: Modify();
                        break;
                    case 5: SelectView();
                        break;
                    default:
                        break;
                }
                Response.End();
            }
        }

        void Delete()
        {
            if (new BLL.User().Delete(int.Parse(Request.Form["id"].ToString()), 0) != 1)
            {
                Response.Write("-1");
            }
        }

        void Modify()
        {
            MODEL.User user = GetValue();
            user.UserId = int.Parse(Request.Form["id"].ToString());
            if (new BLL.User().Modify(user) == 1)
            {
                Response.Write("修改成功");
            }
            else
            {
                Response.Write("修改失败");
            }
        }

        //void FindUserName()
        //{
        //    if (new BLL.User().FindUserName(Request.Form["Username"].ToString()) != null)
        //    {
        //        Response.Write("你要增加的用户名已存在是否继续");
        //    }
        //}

        void Add()
        {
            MODEL.User user = GetValue();
            if (new BLL.User().Add(user) == 1)
            {
                Response.Write("增加成功");
            }
            else
            {
                Response.Write("增加失败");
            }
        }

        private MODEL.User GetValue()
        {
            MODEL.User user = new MODEL.User();
            user.UserName = Request.Form["Username"].ToString();
            user.UserDepId = int.Parse(Request.Form["dep"].ToString());
            user.UserJobId = int.Parse(Request.Form["job"].ToString());
            user.UserTitle = Request.Form["title"].ToString();
            user.UserTime = Request.Form["time"].ToString();
            return user;
        }

        void SelectView()
        {
            string dep=DepView();
            string job = JobView();
            if (dep != null && job != null)
            {
                Response.Write("{dep:'" + dep + "',job:'" + job + "'}");
            }
        }

        string DepView()
        {
             return GetSelect(new BLL.User().DepView());
        }

        string JobView()
        {
            return GetSelect(new BLL.User().JobView());
        }

        string GetSelect(DataTable tb)
        {
            StringBuilder select=new StringBuilder("<ul>");
            for (int i = 0; i < tb.Rows.Count; i++)
			{
                    select.Append("<li><a rel="+tb.Rows[i][0]+">"+tb.Rows[i][1]+"</a></li>");
			}
            select.Append("</ul>");
            return select.ToString();
        }

        void View()
        {
            int pageindex = int.Parse(Request.Form["pageindex"].ToString());
            int pagesize = int.Parse(Request.Form["pagesize"].ToString());
            string querystring = Request.Form["query"].ToString();
            byte flag = Convert.ToByte(Request.Form["UserFlag"].ToString());
            int count = int.Parse(new BLL.User().NumberOfMessages(Request.Form["query"].ToString()).Rows[0][0].ToString());
            string table = GetTable(new BLL.User().View(pageindex, pagesize, querystring, flag, Request.Form["sort"].ToString(), Request.Form["opt"].ToString()));
            string ss = "{count:'"+count+"',table:'"+table+"'}";
            Response.Write(ss);
            Response.End();
        }

        string GetTable(DataTable Table)
        {
            if (Table == null)
            {
                return "出错了";
            }
            string colunmsname = Request.Form["sort"].ToString();
            StringBuilder HtmlTable = new StringBuilder("<table><thead><tr>");
            //IEnumerable<DataRow> tab;

            //Table = tab.CopyToDataTable();
            
            //switch (colunmsname)
            //{
            //    case "员工号": sort = "user_id";
            //        break;
            //    case "姓名": sort = "user_name";
            //        break;
            //    case "部门": sort = "dep_name";
            //        break;
            //    case "岗位": sort = "job_name";
            //        break;
            //    case "职称": sort = "user_title";
            //        break;
            //    case "时间": sort = "user_time";
            //        break;
            //    default: return;
            //}
            if (Request.Form["opt"].ToString() != "")
            {
                SortAsc(colunmsname, HtmlTable);
            }
            else
            {
                SortDec(colunmsname, HtmlTable);
            }
            
            //HtmlTable.Append("<td>姓名</td><td>部门</td><td>岗位</td><td>职称</td><td>时间</td><td>操作</td>");
            HtmlTable.Append("<td>操作</td></tr></thead><tbody>");
            for (int i = 0; i < Table.Rows.Count; i++)
            {
                HtmlTable.Append("<tr id="+Table.Rows[i][0]+">");
                for (int j = 0; j < Table.Columns.Count; j++)
                {
                    HtmlTable.Append("<td>"+Table.Rows[i][j].ToString()+"</td>");
                }
                HtmlTable.Append("</tr>");
            }
            HtmlTable.Append("<tbody></table>");

            return HtmlTable.ToString();
        }

        private static void SortAsc(string colunmsname, StringBuilder HtmlTable)
        {
            if (colunmsname == "user_id")
            {
                HtmlTable.Append("<td>员工号<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>员工号</td>");
            }
            if (colunmsname == "user_name")
            {
                HtmlTable.Append("<td>姓名<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>姓名</td>");
            }
            if (colunmsname == "dep_name")
            {
                HtmlTable.Append("<td>部门<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>部门</td>");
            }
            if (colunmsname == "job_name")
            {
                HtmlTable.Append("<td>岗位<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>岗位</td>");
            }
            if (colunmsname == "user_title")
            {
                HtmlTable.Append("<td>职称<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>职称</td>");
            }
            if (colunmsname == "user_time")
            {
                HtmlTable.Append("<td>时间<span style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-s\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>时间</td>");
            }
        }

        private static void SortDec(string colunmsname, StringBuilder HtmlTable)
        {
            if (colunmsname == "user_id")
            {
                HtmlTable.Append("<td>员工号<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>员工号</td>");
            }
            if (colunmsname == "user_name")
            {
                HtmlTable.Append("<td>姓名<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>姓名</td>");
            }
            if (colunmsname == "dep_name")
            {
                HtmlTable.Append("<td>部门<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>部门</td>");
            }
            if (colunmsname == "job_name")
            {
                HtmlTable.Append("<td>岗位<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>岗位</td>");
            }
            if (colunmsname == "user_title")
            {
                HtmlTable.Append("<td>职称<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>职称</td>");
            }
            if (colunmsname == "user_time")
            {
                HtmlTable.Append("<td>时间<span id=\"asc\" style=\"float:right;\" class=\"ui-icon ui-icon-triangle-1-n\"></span></td>");
            }
            else
            {
                HtmlTable.Append("<td>时间</td>");
            }
        }
    }
}